const fileService = require('../service/file.service');
const userService = require('../service/user.service.js');
const {
  APP_HOST,
  APP_PORT
} = require('../app/config');
const {
  AVATAR_PATH
} = require('../constants/file-path');
class FileController {
  async saveAvatarInfo (ctx, next) {
    // 获取头像相关的信息
    const { filename, mimetype, size } = ctx.req.file;
    const { id } = ctx.user;
    // 将图像信息保存到数据库
    await fileService.createAvatar(filename, mimetype, size, id);
    // 将头像的信息保存到users表中
    // ${AVATAR_PATH}/${filename}
    const avatarUrl = `${APP_HOST}:${APP_PORT}/users/${id}/avatar`;
	  await userService.updateAvatarUrlById(avatarUrl, id);
    ctx.body = "头像上传成功~";
  }
  async savePictureInfo (ctx, next) {
    // 1. 获取数据
    const files = ctx.req.files;
    const { id } = ctx.user;
    const { momentId } = ctx.query;
    // 2. 保存数据库
    for (let file of files) {
      const { filename, mimetype, size } = file;
      await fileService.createFile(filename, mimetype, size, id, momentId);
    }
    ctx.body = "动态上传完成~";
  }
}


module.exports = new FileController();